package cn.tedu.property.management.controller;

import cn.tedu.property.management.pojo.dto.VisitPeopleAddNewDTO;
import cn.tedu.property.management.pojo.dto.VisitPeopleUpdateDTO;
import cn.tedu.property.management.pojo.vo.VisitPeopleListItemVO;
import cn.tedu.property.management.pojo.vo.VisitPeopleStandardVO;
import cn.tedu.property.management.service.IVisitPeopleService;
import cn.tedu.property.management.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Validated
@RestController
@Api(tags = "访客管理模块")
@RequestMapping("/visitPeoples")
public class VisitPeopleController {

    @Autowired
    private IVisitPeopleService visitPeopleService;

    @PostMapping( "/add-new")
    @ApiOperation("添加访客")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid VisitPeopleAddNewDTO visitPeopleAddNewDTO){
        log.debug("开始处理【添加访客】的业务,参数:{}",visitPeopleAddNewDTO);
        visitPeopleService.addNew(visitPeopleAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除访客")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "访客ID", required = true, example = "6", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1,message = "删除失败,访客id不存在") @PathVariable Long id) {
        log.debug("开始处理【根据id删除访客】的业务，参数：{}", id);
        visitPeopleService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改访客详情")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@PathVariable Long id, VisitPeopleUpdateDTO visitPeopleUpdateDTO) {
        log.debug("开始处理修改访客详情业务");
        visitPeopleService.updateInfoById(id,visitPeopleUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查看详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "访客ID", required = true, example = "6", dataType = "long")
    public JsonResult<VisitPeopleStandardVO> getStandardById(@Range(min = 1,message = "删除失败,访客id不存在") @PathVariable Long id) {
        log.debug("开始处理根据ID查询数据详情");
        VisitPeopleStandardVO visitPeopleStandardVO = visitPeopleService.getStandardById(id);
        return JsonResult.ok(visitPeopleStandardVO);
    }

    @GetMapping("/list")
    @ApiOperation("查看访客列表")
    @ApiOperationSupport(order = 410)
    public JsonResult<List<VisitPeopleListItemVO>> list() {
        List<VisitPeopleListItemVO> list = visitPeopleService.list();
        return JsonResult.ok(list);
    }
}
